package com.cwd.arm.domain.entity;

import com.cwd.arm.common.util.BigDecimalUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:开票信息
 * Created on 2021年11月16日
 *
 * @author yan
 */
@Data
public class TaxEntity {

    /**
     *
     */
    private Long id;

    /**
     * 项目id
     */
    private Long projectId;

    /**
     * 收款节点id
     */
    private Long receiveNodeId;

    /**
     * 发票税号
     */
    private String taxNo;

    /**
     * 发票日期
     */
    private java.util.Date taxDate;

    /**
     * 是否生效
     */
    private Boolean hasEffect;

    /**
     * 税点
     */
    private java.math.BigDecimal taxRate;

    /**
     * 含税金额
     */
    private java.math.BigDecimal taxAmount;

    /**
     * 已收金额
     */
    private java.math.BigDecimal receivedAmount;

    private List<ReceiveRecordEntity> diffReceiveRecordEntityList;

    private ReceiveRecordEntity oldReceiveRecordEntity;

    private ReceiveRecordEntity newReceiveRecordEntity;


    /**
     * 回退操作
     */
    public void undo() {
        taxAmount = BigDecimalUtil.minus(taxAmount);
    }

    public void addRecord(ReceiveRecordEntity receiveRecordEntity) {
        receivedAmount = BigDecimalUtil.add(receiveRecordEntity.getAmount(), receivedAmount);
        newReceiveRecordEntity = receiveRecordEntity;
    }

    public void updateRecord(ReceiveRecordEntity oldReceiveRecordEntity
            , ReceiveRecordEntity newReceiveRecordEntity) {
        newReceiveRecordEntity.initAmount(oldReceiveRecordEntity);
        oldReceiveRecordEntity.undo();
        BigDecimal diffAmount = BigDecimalUtil.add(oldReceiveRecordEntity.getAmount(), newReceiveRecordEntity.getAmount());
        receivedAmount = BigDecimalUtil.add(receivedAmount, diffAmount);
        this.oldReceiveRecordEntity = oldReceiveRecordEntity;
        this.newReceiveRecordEntity = newReceiveRecordEntity;
    }

    public void delRecord(ReceiveRecordEntity receiveRecordEntity) {
        receiveRecordEntity.undo();
        receivedAmount = BigDecimalUtil.add(receivedAmount, receiveRecordEntity.getAmount());
        newReceiveRecordEntity = receiveRecordEntity;
    }

    public List<ReceiveRecordEntity> getDiffReceiveRecordEntityList() {
        diffReceiveRecordEntityList = new ArrayList<>(2);
        if (newReceiveRecordEntity != null) {
            diffReceiveRecordEntityList.add(newReceiveRecordEntity);
        }
        if (oldReceiveRecordEntity != null) {
            diffReceiveRecordEntityList.add(oldReceiveRecordEntity);
        }
        return diffReceiveRecordEntityList;
    }

    public BigDecimal getTaxUnreceiveAmount() {
        return BigDecimalUtil.sub(taxAmount, receivedAmount);
    }

    public void init(TaxEntity oldTaxEntity){
        if(oldTaxEntity!=null){
            if(receivedAmount == null){
                receivedAmount = oldTaxEntity.getReceivedAmount();
            }
            if(taxAmount == null){
                taxAmount = oldTaxEntity.getTaxAmount();
            }
            if(taxNo == null){
                taxNo = oldTaxEntity.getTaxNo();
            }
            if(taxDate == null){
                taxDate = oldTaxEntity.getTaxDate();
            }
            if(taxRate == null){
                taxRate = oldTaxEntity.getTaxRate();
            }
        }
    }

    public boolean hasChangeReceiveNode(Long oldReceiveNodeId){
        if(receiveNodeId!=null && receiveNodeId.compareTo(oldReceiveNodeId)!=0){
            return true;
        }
        return false;
    }

}
